define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/index' + location.search,
                    add_url: 'activity/add',
                    edit_url: 'activity/edit',
                    del_url: 'activity/del',
                    multi_url: 'activity/multi',
                    import_url: 'activity/import',
                    dragsort_url: '',
                    table: 'activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                // commonSearch: false,
                search:false,
                showToggle: false,
                showColumns: false,
                visible: false,
                showExport: false,
                pageSize: 10,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'area_name', title: '所屬區域', operate: false},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'activitycategory.name', title: '活動分類', operate: false},
                        {field: 'click', title: '瀏覽量', sortable: true},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                            this.buttons = [
                            
                                  {
                                    name: 'activity_code',
                                    text: '分享二維碼',
                                    title: '分享二維碼',
                                    classname: 'btn btn-xs btn-info',
                                    extend:'download="活動分享_'+row.id+'.png"',
                                    icon: '',
                                    url: function (row){
                                    // row 为行数据
                                      return row.activity_code;
                                    } ,
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                          return true;
                                    }
                                },
                                {
                                    name: 'edit',
                                    text: '編輯',
                                    title: '編輯',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: '',
                                    url: 'activity/edit',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_edit ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'activity_comment',
                                    text: '評論列表',
                                    title: '評論列表',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: '',
                                    url: 'activity_comment/index',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_comment ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'del',
                                    text: '删除',
                                    title: '删除',
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: '',
                                    url: 'activity/del',
                                    confirm: '确认删除吗?',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if ( Config.activity_del ) {
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                }
                                ];
                                return Table.api.formatter.buttons.call(this,value,row,index)
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

          // 明細
        sign: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/sign/ids/'+ Config.ids + location.search,
                    add_url: 'staff/add',
                    edit_url: 'staff/edit',
                    del_url: 'staff/del',
                    multi_url: 'staff/multi',
                    import_url: 'staff/import',
                    table: 'staff',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:true,
                commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '義工時數_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'vo_name', title: __('義工')},
                         {field: 'center_name', title: __('中心'), operate: 'LIKE'},
                        {field: 'activity_name', title: __('活動名稱'), operate: 'LIKE'},
                       
                        {field: 'hours', title: __('時數'), operate: false},
                        {field: 'create_time', title: __('報名時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                      {field: 'sign_time', title: __('簽到時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    
                    ]
                ]
            });
              table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
            // 明細-用戶
        sign_user: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/sign_user/ids/'+ Config.ids + location.search,
                    add_url: 'staff/add',
                    edit_url: 'staff/edit',
                    del_url: 'staff/del',
                    multi_url: 'staff/multi',
                    import_url: 'staff/import',
                    table: 'staff',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:true,
                commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '簽到記錄_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                     
                        {field: 'activity_name', title: __('活動名稱'), operate: 'LIKE'},

                        {field: 'sign_time', title: __('簽到時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
              table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
            // 名單
        volunteer_list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/volunteer_list/ids/'+ Config.ids + location.search,
                    add_url: 'staff/add',
                    edit_url: 'staff/edit',
                    del_url: 'staff/del',
                    multi_url: 'staff/multi',
                    import_url: 'staff/import',
                    table: 'colunteer_list',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:true,
                commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '義工時數_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['avatar'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'vo_name', title: __('義工')},
                         {field: 'center_name', title: __('中心'), operate: 'LIKE'},
                        {field: 'activity_name', title: __('活動名稱'), operate: 'LIKE'},
                       
                        {field: 'hours', title: __('時數'), operate: false},
                        {field: 'createtime', title: __('簽到時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });
              table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'activity/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'activity/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'activity/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});